<?php

namespace App\Http\Controllers\Api\V2;

use App\Http\Controllers\Api\BaseController;
use Illuminate\Http\Request;
use EasyWeChat;
use Log;

class WechatController extends BaseController
{
    public function serve()
    {
        $app = EasyWeChat::officialAccount(); // 公众号
        Log::info('request arrived.');
        $app->server->push(function ($message) use($app){
            // $message['FromUserName'] // 用户的 openid
            // $message['MsgType'] // 消息类型：event, text....
            $openid = $message['FromUserName'];
            switch ($message['MsgType'])
            {
                case 'event':
                    if($message['Event'] == 'subscribe')
                    {
                        //订阅
                        $ret = "Hi，初次见面的老朋友

在这里，你可以用很实惠的价格买到心怡的好书，也可以将自己闲置书卖给我们。

用卖书到账的金额买书，可以享8.7折优惠，当然也可以随时提现！

如有任何疑问或建议，都可以随时找客服沟通。

客服微信号：kuaiyu-kefu";

                        $row = \DB::table('wechat_users')->where('openid',$openid)->first();
                        if(empty($row))
                        {
                            $user = $app->user->get($openid);
                            $data = ['openid' => $openid, 'unionid' => $user['unionid'], 'nickname' => $user['nickname']];
                            \DB::table('wechat_users')->insert($data);
                        }

                        return $ret;
                    }
                    elseif($message['Event'] == 'unsubscribe')
                    {
                        //取消订阅
                        \DB::table('wechat_users')->where('openid',$openid)->delete();
                    }
                    break;
                case 'text':
                    return '收到文字消息';
                    break;
            }

            $ret = "ok";
            return $ret;
        });

        // 在 laravel 中：
        $response = $app->server->serve();
        return $response;
    }

    public function menu()
    {
        $app = EasyWeChat::officialAccount(); // 公众号
        $buttons = [
            [
                "type"      => "miniprogram",
                "name"      => "二手书店",
                "url"       => "https://test.dingdangtoy.com",
                "key"       => "",
                "appid"     => "wx0ee64e1e48fc9307",
                "pagepath"  => "pages/index/index"
            ],
            [
                "type"      => "miniprogram",
                "name"      => "我要卖书",
                "url"       => "https://test.dingdangtoy.com",
                "key"       => "",
                "appid"     => "wx0ee64e1e48fc9307",
                "pagepath"  => "pages/sale/index/index"
            ],
            [
                "name"       => "答疑",
                "sub_button" => [
                    [
                        "type"      => "miniprogram",
                        "name"      => "品相说明",
                        "url"       => "https://test.dingdangtoy.com",
                        "appid"     => "wx0ee64e1e48fc9307",
                        "pagepath"  => "pages/pinxiang-intro/pinxiang-intro"
                    ],
                    [
                        "type"      => "miniprogram",
                        "name"      => "卖书规则",
                        "url"       => "https://test.dingdangtoy.com",
                        "appid"     => "wx0ee64e1e48fc9307",
                        "pagepath"  => "pages/sale/rules/rules"
                    ]
                ],
            ]
        ];
        $app->menu->create($buttons);
        echo 'ok';
    }
}
